$(function() {
	admin.shopCommunity.init();
});
Namespace('admin.shopCommunity',{
	init:function() {
		easyui.util.initDatagrid('#datagrid', {
			onDblClickRow : function() {
				
			}
		});
	},

	doUpdate:function(shopId) {
		if(!$('#data-form').form('validate')) return;
		var data=$('#data-form').form('jsonObject');
		if (data.communityIdList == null || data.communityIdList == '') {
			top.showInfo('请选择社区!');
			return;
		}
		var communityIdList = "";
		if(typeof(data.communityIdList) == 'string'){
			communityIdList = data.communityIdList;
		}else{
			var len = data.communityIdList.length;
			for(var i=0;i<len;i++){
				if (communityIdList != '') {
					communityIdList += ';';
				}
				communityIdList += data.communityIdList[i];
  			}
		}
		data = 'shopId='+shopId+'&communityIds='+communityIdList;
		$.post(contextPath+'/mall/shop/community/edit',data,function(result) {
			if(result.code==1) {
				$('#saveCommunity').hide();
				top.showInfo('修改成功');
			} else {
				top.showInfo('修改失败:'+result.msg);
			}
		},'json');
	},
	
	chooseCommunity:function() { //选择社区
		$('#saveCommunity').show();
		getDialog('query-dialog').dialog({
			href:contextPath+'/community/base/choose',
			title: '点击选择社区',
		    width: 900,
		    height: 500,
		    buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function() {
					admin.shopCommunity.addCommunity();
					$('#query-dialog').dialog('close');
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#query-dialog').dialog('close');
				}
			}]
		});
	},
	addCommunity:function() { //选择社区
		var rows = $('#datagrid2').datagrid('getSelections');
		if(rows.length==0){  
			alert("请至少选择一行数据!");  
			return false;  
		}
		var len = rows.length;
		for (var i = 0; i < len; i++) {
			var html = [];
			html.push('<tr><td><input type="hidden" id="'+rows[i].id+'" name="communityIdList" value="'+rows[i].id+'"/><dl class="t_user"><dt><p class="img"><img src="'+rows[i].pathImgs+'" alt="" /></p></dt><dd>'+rows[i].name+'</dd></dl></td>');
			html.push('<td>'+rows[i].newAt+'</td>');
			html.push('<td>'+rows[i].id+'</td>');
			var status="";
			if(rows[i].status == 1){
				status="待确认";
			}else if(rows[i].status == 2){
				status="已确认";
			}else if(rows[i].status == 3){
				status="已失效";
			}
			html.push('<td>'+status+'</td>');
			html.push('<td><a href="javascript:;" class="cblue" onclick="admin.shopCommunity.removeTr(this)">删除</a></tr>');
			$('#aa').append(html.join(''));
		}
	},
	removeTr:function(obj){
		$('#saveCommunity').show();
		$(obj).closest('tr').remove();
	},
});